﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net;
using OAuthLib;
using System.IO;
using System.Xml.Linq;
using System.Threading.Tasks;
using SinaWeiboClientV1;
using RestAPICore;

namespace SinaJDCBClient.Test
{

    public static class DoubleExtension
    {
        public static string Conv2WeiboId(this double value)
        {
            return Convert.ToInt64(value).ToString();
        }
    }

    public static class StreamExtension
    {
        public static string SaveAs(this JsonResponse<Stream> response, string filename = null, bool overwrite = true)
        {
            using (var stream = response.Value)
            {
                using (var sr = new StreamReader(stream, Encoding.UTF8))
                {
                    var result = sr.ReadToEnd();

                    if (string.IsNullOrEmpty(filename))
                        filename = System.Guid.NewGuid().ToString("N") + ".json";

                    var myDocFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                    var workFolder = Path.Combine(myDocFolder, "JDCB", DateTime.Now.ToString("yyyyMMdd"));

                    if (!Directory.Exists(workFolder))
                        Directory.CreateDirectory(workFolder);

                    filename = System.IO.Path.Combine(workFolder, filename);
                    var subFolder = Path.GetDirectoryName(filename);
                    if (!Directory.Exists(subFolder))
                        Directory.CreateDirectory(subFolder);

                    var exists = (File.Exists(filename));
                    if ((exists && overwrite) || (!exists))
                    {
                        System.IO.File.WriteAllText(filename, result);
                    }

                    return result;
                }
            }
        }
    }
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class SinaWeiboClientV1Test_getjson
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        private WeiboClient _sinaWeibo;

        public SinaWeiboClientV1Test_getjson()
        {
            _sinaWeibo = new WeiboClient();
            var token = TokenHelper.CreateOAuthToken("967389259", "001379b11292669a5399131cfc5b806d", "jdcbtest@sina.com", "jdcbtest");
            _sinaWeibo.CurrentAuthToken = token;
        }

        [ClassInitialize()]
        public static void SetProxy(TestContext testContext)
        {
            var ipAddressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
            if (ipAddressList.ToList().Any(ip => ip.ToString().StartsWith("10.1.38.")))
            {
                HttpWebRequest.DefaultWebProxy = new WebProxy("http://10.1.40.253:8080", true);
                HttpWebRequest.DefaultWebProxy.Credentials = new NetworkCredential { UserName = "fangxing", Password = "fangxing" };
            }
        }

        #region statuses
        [TestMethod]
        public void TestStatusesPublicTimeline()
        {
            var request = new SinaWeiboClientV1.StatusesPublicTimelineRequest() { Count = "2" };
            var response = _sinaWeibo.RequestStatusesPublicTimeline(request);
            var result = response.SaveAs("statuses/public_timeline.Response.json");
        }

        [TestMethod]
        public void TestStatusesFriends()
        {
            var request = new SinaWeiboClientV1.StatusesFriendsRequest() { UserId = _sinaWeibo.CurrentAuthToken.ContextData["UserID"] as string, Count = "1" };
            var response = _sinaWeibo.RequestStatusesFriends(request);
            var result = response.SaveAs("statuses/friends.Response.json");
        }

        [TestMethod]
        public void TestStatusesUpdate()
        {
            var msg = "中文" + DateTime.Now.GetHashCode();
            var request = new SinaWeiboClientV1.StatusesUpdateRequest { Status = OAuthLib.HttpEncoder.UrlEncode(msg) };
            var response = _sinaWeibo.RequestStatusesUpdate(request);
            var result = response.SaveAs("statuses/update.Response.json");
        }

        [TestMethod]
        public void TestStatusesComment()
        {
            var msg = "TestRequestStatusesComment_" + +DateTime.Now.GetHashCode();
            var request = new SinaWeiboClientV1.StatusesUpdateRequest { Status = OAuthLib.HttpEncoder.UrlEncode(msg) };
            var response = _sinaWeibo.ExecuteStatusesUpdate(request);
            var statusId = response.Value.Id.Conv2WeiboId();

            var requestComment = new SinaWeiboClientV1.StatusesCommentRequest { Id = statusId, Comment = "comment_" + DateTime.Now.GetHashCode() };
            var result = _sinaWeibo.RequestStatusesComment(requestComment).SaveAs("statuses/comment.Response.json");
        }

        [TestMethod]
        public void TestStatusesCommentDestory()
        {
            var msg = "TestStatusesCommentDestory_" + DateTime.Now.GetHashCode();
            var request = new SinaWeiboClientV1.StatusesUpdateRequest { Status = OAuthLib.HttpEncoder.UrlEncode(msg) };
            var response = _sinaWeibo.ExecuteStatusesUpdate(request);
            var statusId = response.Value.Id.Conv2WeiboId();

            var requestComment = new SinaWeiboClientV1.StatusesCommentRequest { Id = statusId, Comment = "comment_" + DateTime.Now.GetHashCode() };
            var responseComment = _sinaWeibo.ExecuteStatusesComment(requestComment);

            var commentId = responseComment.Value.Id.Conv2WeiboId();
            var requestDestoryComment = new SinaWeiboClientV1.StatusesCommentDestroyRequest { Id = commentId.ToString() };
            var responseDestoryComment = _sinaWeibo.RequestStatusesCommentDestroy(requestDestoryComment);
            var result = responseDestoryComment.SaveAs("statuses/comment_destroy.Request.json");
        }

        [TestMethod]
        public void TestStatusesDestory()
        {
            var msg = "TestStatusesDestory_" + DateTime.Now.GetHashCode();
            var request = new SinaWeiboClientV1.StatusesUpdateRequest { Status = OAuthLib.HttpEncoder.UrlEncode(msg) };
            var response = _sinaWeibo.ExecuteStatusesUpdate(request);
            var statusId = response.Value.Id.Conv2WeiboId();

            var requestStatusDestory = new SinaWeiboClientV1.StatusesDestroyRequest { Id = statusId };
            var responseStatusDestory = _sinaWeibo.RequestStatusesDestroy(requestStatusDestory);
            var result = responseStatusDestory.SaveAs("statuses/destory.Response.json");
        }

        [TestMethod]
        public void TestCommentsDestoryBatch()
        {
            var msg = "TestStatusesCommentDestory_" + DateTime.Now.GetHashCode();
            var request = new SinaWeiboClientV1.StatusesUpdateRequest { Status = OAuthLib.HttpEncoder.UrlEncode(msg) };
            var response = _sinaWeibo.ExecuteStatusesUpdate(request);
            var statusId = response.Value.Id.Conv2WeiboId();

            var requestComment1 = new SinaWeiboClientV1.StatusesCommentRequest { Id = statusId, Comment = "comment_" + DateTime.Now.GetHashCode() };
            var responseComment1 = _sinaWeibo.ExecuteStatusesComment(requestComment1);

            var requestComment2 = new SinaWeiboClientV1.StatusesCommentRequest { Id = statusId, Comment = "comment_" + DateTime.Now.GetHashCode() };
            var responseComment2 = _sinaWeibo.ExecuteStatusesComment(requestComment2);

            var ids = responseComment1.Value.Id.Conv2WeiboId() + "," + responseComment2.Value.Id.Conv2WeiboId();
            var requestCommentsDestoryBatch = new SinaWeiboClientV1.StatusesCommentDestroyBatchRequest { Ids = ids };
            var responseCommentsDestoryBatch = _sinaWeibo.RequestStatusesCommentDestroyBatch(requestCommentsDestoryBatch);
            var result = responseCommentsDestoryBatch.SaveAs("statuses/comment_destroy_batch.json");
        }

        [TestMethod]
        public void TestStatusesCommentsByMe()
        {
            var requestCommentsByMe = new SinaWeiboClientV1.StatusesCommentsByMeRequest { Count = "2" };
            var responseCommentsByMe = _sinaWeibo.RequestStatusesCommentsByMe(requestCommentsByMe);
            var result = responseCommentsByMe.SaveAs("statuses/comments_by_me.Response.json");
        }

        [TestMethod]
        public void TestStatusesCommentsTimeline()
        {
            var requestCommentsTimeline = new SinaWeiboClientV1.StatusesCommentsTimelineRequest { Count = "1" };
            var responseCommentsTimeline = _sinaWeibo.RequestStatusesCommentsTimeline(requestCommentsTimeline);
            var result = responseCommentsTimeline.SaveAs("statuses/comments_timeline.Response.json");
        }

        [TestMethod]
        public void TestStatusesCommentsToMe()
        {
            var requestCommentsToMe = new SinaWeiboClientV1.StatusesCommentsToMeRequest { Count = "2" };
            var responseCommentsToMe = _sinaWeibo.RequestStatusesCommentsToMe(requestCommentsToMe);
            var result = responseCommentsToMe.SaveAs("statuses/comments_to_me.json");
        }

        [TestMethod]
        public void TestStatusesCounts()
        {
            var requestPublicTimeline = new SinaWeiboClientV1.StatusesPublicTimelineRequest { Count = "2" };
            var responsePublicTimeline = _sinaWeibo.ExecuteStatusesPublicTimeline(requestPublicTimeline);
            var ids = string.Join(",", responsePublicTimeline.Value.Select(s => s.Id.Conv2WeiboId()).ToArray());
            
            var requestStatusCounts = new SinaWeiboClientV1.StatusesCountsRequest { Ids = ids };
            var responseStatusCounts = _sinaWeibo.RequestStatusesCounts(requestStatusCounts);
            var result = responseStatusCounts.SaveAs("statuses/counts.Response.json");
        }

        [TestMethod]
        public void TestStatusesFollowers()
        {
            var requestFollowers = new SinaWeiboClientV1.StatusesFollowersRequest { UserId = _sinaWeibo.CurrentAuthToken.ContextData["UserID"] as string };
            var responseFollowers = _sinaWeibo.RequestStatusesFollowers(requestFollowers);
            var result = responseFollowers.SaveAs("statuses/followers.Response.json");
        }

        [TestMethod]
        public void TestStatusesFriendsTimeline()
        {
            var requestFriendsTimeline = new SinaWeiboClientV1.StatusesFriendsTimelineRequest { Count = "2" };
            var responseFriendsTimeline = _sinaWeibo.RequestStatusesFriendsTimeline(requestFriendsTimeline);
            var result = responseFriendsTimeline.SaveAs("statuses/friends_timeline.Response.json");
        }

        [TestMethod]
        public void TestStatusesMentions()
        {
            var requestMentions = new SinaWeiboClientV1.StatusesMentionsRequest { Count = "2" };
            var responseMentions = _sinaWeibo.RequestStatusesMentions(requestMentions);
            var result = responseMentions.SaveAs("statuses/mentions.Response.json");
        }

        [TestMethod]
        public void TestStatusesReply()
        {
            var requestCommentsToMe = new SinaWeiboClientV1.StatusesCommentsToMeRequest { Count = "2" };
            var responseCommentsToMe = _sinaWeibo.ExecuteStatusesCommentsToMe(requestCommentsToMe);
            Assert.IsTrue(responseCommentsToMe.Value.Count() > 0);

            var statusId = responseCommentsToMe.Value.First().Id.Conv2WeiboId();
            var commentId = responseCommentsToMe.Value.First().Id.Conv2WeiboId();

            var requestReply = new SinaWeiboClientV1.StatusesReplyRequest { Id = statusId, Cid = commentId, Comment = "reply_" + DateTime.Now.GetHashCode() };
            var responseReply = _sinaWeibo.RequestStatusesReply(requestReply);
            var result = responseReply.SaveAs("statuses/reply.Response.json");
        }

        [TestMethod]
        public void TestStatusesRepost()
        {
            var requestStatusUpdate = new SinaWeiboClientV1.StatusesUpdateRequest { Status = "TestStatusesRepost_" + DateTime.Now.GetHashCode() };
            var responseStatusUpdate = _sinaWeibo.ExecuteStatusesUpdate(requestStatusUpdate);
            var statusId = responseStatusUpdate.Value.Id.Conv2WeiboId();

            var requestRepost = new SinaWeiboClientV1.StatusesRepostRequest { Id = statusId, IsComment = "1", Status = "TestStatusesRepost_" + DateTime.Now.GetHashCode() };
            var responseRepost = _sinaWeibo.RequestStatusesRepost(requestRepost);
            var result = responseRepost.SaveAs("statuses/repost.Response.json");
        }

        [TestMethod]
        public void TestStatusesRepostByMe()
        {
            var requestCommentsToMe = new SinaWeiboClientV1.StatusesCommentsToMeRequest { Count = "1" };
            var responseCommentsToMe = _sinaWeibo.ExecuteStatusesCommentsToMe(requestCommentsToMe);
            var userId = responseCommentsToMe.Value.First().Id.Conv2WeiboId();

            var requestRepostByMe = new SinaWeiboClientV1.StatusesRepostByMeRequest { Id = userId, Count = "2" };
            var responseRepostByMe = _sinaWeibo.RequestStatusesRepostByMe(requestRepostByMe);
            var result = responseRepostByMe.SaveAs("statuses/repost_by_me.Response.json");
        }

        [TestMethod]
        public void TestStatusesRepostTimeline()
        {
            var requestStatusUpdate = new SinaWeiboClientV1.StatusesUpdateRequest { Status = "TestStatusesRepostTimeline_" + DateTime.Now.GetHashCode() };
            var responseStatusUpdate = _sinaWeibo.ExecuteStatusesUpdate(requestStatusUpdate);
            var statusId = responseStatusUpdate.Value.Id.Conv2WeiboId();

            var requestRepost = new SinaWeiboClientV1.StatusesRepostRequest { Id = statusId };
            var responseRepost = _sinaWeibo.RequestStatusesRepost(requestRepost);

            var requestRepostTimeline = new SinaWeiboClientV1.StatusesRepostTimelineRequest { Id = statusId };
            var responseRepostTimeline = _sinaWeibo.RequestStatusesRepostTimeline(requestRepostTimeline);
            var result = responseRepostTimeline.SaveAs("statuses/repost_timeline.Reponse.json");            
        }

        [TestMethod]
        public void TestStatusesShow()
        {
            var requestStatusUpdate = new SinaWeiboClientV1.StatusesUpdateRequest { Status = "TestStatusesShow_" + DateTime.Now.GetHashCode() };
            var responseStatusUpdate = _sinaWeibo.ExecuteStatusesUpdate(requestStatusUpdate);
            var statusId = responseStatusUpdate.Value.Id.Conv2WeiboId();

            var requestShow = new SinaWeiboClientV1.StatusesShowRequest { Id = statusId };
            var responseShow = _sinaWeibo.RequestStatusesShow(requestShow);
            var result = responseShow.SaveAs("statuses/show.Response.json");
        }

        [TestMethod]
        public void TestStatusesUnread()
        {
            var requestUnread = new SinaWeiboClientV1.StatusesUnreadRequest { WithNewStatus = "1" };
            var responseUnread = _sinaWeibo.RequestStatusesUnread(requestUnread);
            var result = responseUnread.SaveAs("statuses/unread.Response.json");
        }

        [TestMethod]
        public void TestStatusesResetCount()
        {
            var requestResetCount = new SinaWeiboClientV1.StatusesResetCountRequest { Type = "1" };
            var responseResetCount = _sinaWeibo.RequestStatusesResetCount(requestResetCount);
            var result = responseResetCount.SaveAs("statuses/reset_count.Response.json");
        }

        [TestMethod]
        public void TestStatusesUserTimeline()
        {
            var requestStatusUpdate = new SinaWeiboClientV1.StatusesUpdateRequest { Status = "TestStatusesUserTimeline_" + DateTime.Now.GetHashCode() };
            var responseStatusUpdate = _sinaWeibo.ExecuteStatusesUpdate(requestStatusUpdate);
            var statusId = responseStatusUpdate.Value.Id.Conv2WeiboId();

            var requestUserTimeline = new SinaWeiboClientV1.StatusesUserTimelineRequest { Id = _sinaWeibo.CurrentAuthToken.ContextData["UserID"] as string, Count = "2", MaxId = statusId };
            var responseUserTimeline = _sinaWeibo.RequestStatusesUserTimeline(requestUserTimeline);
            var result = responseUserTimeline.SaveAs("statuses/user_timeline.Response.json");
        }

        [TestMethod]
        public void TestStatusesUpload()
        {
            var requestUpload = new SinaWeiboClientV1.StatusesUploadRequest { Status = "TestStatusesUpload_" + DateTime.Now.GetHashCode() };
            requestUpload.RequestData.UploadFiles.Add("pic", new RestAPICore.FileUploadInfo
            {
                LocalPath = "e:\test.png",
                StreamFactory = Task.Factory.StartNew<Stream>(() => File.OpenRead(@"e:\test.png"))
            });
            var responseUpload = _sinaWeibo.RequestStatusesUpload(requestUpload);
            var result = responseUpload.SaveAs("statuses/upload.Response.json");
        }
        #endregion

        #region users
        [TestMethod]
        public void TestUsersShow()
        {
            var request = new SinaWeiboClientV1.UsersShowRequest { Id = _sinaWeibo.CurrentAuthToken.ContextData["UserID"] as string };
            var response = _sinaWeibo.ExecuteUsersShow(request);
            Assert.AreEqual("JDCB_001", response.Value.ScreenName);
        }
        
        [TestMethod]
        public void TestUsersHot()
        {
            var requestUsersHot = new SinaWeiboClientV1.UsersHotRequest { Category = "ent" };
            var responseUsersHot = _sinaWeibo.RequestUsersHot(requestUsersHot);
            var result = responseUsersHot.SaveAs("users/hot.Response.json");

            var requestUsersHot2 = new SinaWeiboClientV1.UsersHotRequest { Category = "music" };
            var responseUsersHot2 = _sinaWeibo.RequestUsersHot(requestUsersHot2);
            var result2 = responseUsersHot2.SaveAs("users/hot.Response.json2");
        }

        [TestMethod]
        public void TestUsersSuggestions()
        {
            var requestUsersSuggestionsWithReason = new SinaWeiboClientV1.UsersSuggestionsRequest { WithReason = "1" };
            var responseUserSuggestionsWithReason = _sinaWeibo.RequestUsersSuggestions(requestUsersSuggestionsWithReason);
            var result1 = responseUserSuggestionsWithReason.SaveAs("users/suggestions_with_reason.Response.json");

            var requestUsersSuggestionsWithoutReason = new SinaWeiboClientV1.UsersSuggestionsRequest { WithReason = "0" };
            var responseUsersSuggestionsWithoutReason = _sinaWeibo.RequestUsersSuggestions(requestUsersSuggestionsWithoutReason);
            var result2 = responseUsersSuggestionsWithoutReason.SaveAs("users/suggestions_without_reason.Response.json");
        }
        #endregion

        #region friendships
        [TestMethod]
        public void TestFriendshipsDestory()
        {
            var requestFriendshipsDestory = new SinaWeiboClientV1.FriendshipsDestroyRequest { Id = "1795200753" };
            var responseFriendshipsDestory = _sinaWeibo.RequestFriendshipsDestroy(requestFriendshipsDestory);
            var result = responseFriendshipsDestory.SaveAs("friendships\\destory.Response.json");
        }

        [TestMethod]
        public void TestFriendshipsCreate()
        {            
            var requestFriendshipsCreate = new SinaWeiboClientV1.FriendshipsCreateRequest { Id = "1795200753" };
            var responseFriendshipsCreate = _sinaWeibo.RequestFriendshipsCreate(requestFriendshipsCreate);
            var result = responseFriendshipsCreate.SaveAs("friendships\\create.Response.json");
        }

        [TestMethod]
        public void TestFriendshipsExists()
        {
            var requestFriendshipsExists = new SinaWeiboClientV1.FriendshipsExistsRequest { UserA = "1795200753", UserB = "1735863012" }; ;
            var responseFriendshipsExists = _sinaWeibo.RequestFriendshipsExists(requestFriendshipsExists);
            var result = responseFriendshipsExists.SaveAs("friendships\\exists.Response.json");
        }

        [TestMethod]
        public void TestFriendshipsShow()
        {
            var requestFriendshipsShow = new SinaWeiboClientV1.FriendshipsShowRequest { SourceId = "1795200753", TargetId = "1735863012" };
            var responseFriendshipShow = _sinaWeibo.RequestFriendshipsShow(requestFriendshipsShow);
            var result = responseFriendshipShow.SaveAs("friendships\\show.Response.json");
        }
        #endregion

        #region trends
        [TestMethod]
        public void TestRequestTrendsStatuses()
        {
            var request = new SinaWeiboClientV1.TrendsStatusesRequest { TrendName = HttpEncoder.UrlEncode("云计算") };
            var response = _sinaWeibo.RequestTrendsStatuses(request);
            var result = response.SaveAs("trends/statuses.Response.json");
        }

        [TestMethod]
        public void TestTrends()
        {
            var request = new SinaWeiboClientV1.TrendsTrendsRequest { Count = "2", UserId = _sinaWeibo.CurrentAuthToken.ContextData["UserID"] as string };
            var response = _sinaWeibo.RequestTrendsTrends(request);
            var result = response.SaveAs("trends/trends.Response.json");
        }

        [TestMethod]
        public void TestTrendsFollow()
        {
            var request = new SinaWeiboClientV1.TrendsFollowRequest { TrendName = HttpEncoder.UrlEncode("云计算") };
            var response = _sinaWeibo.RequestTrendsFollow(request);
            var result = response.SaveAs("trends/follow.Response.json");
        }

        [TestMethod]
        public void TestTrendsDestory()
        {
            var requestTrendFollow = new SinaWeiboClientV1.TrendsFollowRequest { TrendName = HttpEncoder.UrlEncode("金陵十三钗") };
            var responseTrendFollow = _sinaWeibo.ExecuteTrendsFollow(requestTrendFollow);
            var trendId = responseTrendFollow.Value.Topicid;

            var requestDestoryTrend = new SinaWeiboClientV1.TrendsDestroyRequest { TrendId = trendId };
            var responseDestoryTrend = _sinaWeibo.RequestTrendsDestroy(requestDestoryTrend);
            var result = responseDestoryTrend.SaveAs("trends/destory.Response.json");
        }

        [TestMethod]
        public void TestTrendsHourly()
        {
            var requestTrendsHourly = new SinaWeiboClientV1.TrendsHourlyRequest();
            var responseTrendsHourly = _sinaWeibo.RequestTrendsHourly(requestTrendsHourly);
            var result = responseTrendsHourly.SaveAs("trends/hourly.Response.json");
        }

        [TestMethod]
        public void TestTrendsDaily()
        {
            var requestTrendsDaily = new SinaWeiboClientV1.TrendsDailyRequest();
            var responseTrendsDaily = _sinaWeibo.RequestTrendsDaily(requestTrendsDaily);
            var result = responseTrendsDaily.SaveAs("trends/daily.Response.json");
        }

        [TestMethod]
        public void TestTrendsWeekly()
        {
            var requestTrendsWeekly = new SinaWeiboClientV1.TrendsWeeklyRequest();
            var responseTrendsWeekly = _sinaWeibo.RequestTrendsWeekly(requestTrendsWeekly);
            var result = responseTrendsWeekly.SaveAs("trends/weekly.Response.json");
        }
        #endregion

        #region short_url
        [TestMethod]
        public void TestShortUrl()
        {
            var url = "http://www.sina.com";
            var requestShortUrl = new SinaWeiboClientV1.ShortUrlShortenRequest { UrlLong = new List<string> { url } };
            var responseShortUrl = _sinaWeibo .RequestShortUrlShorten(requestShortUrl);
            var result = responseShortUrl.SaveAs("short_url/shorten.Response.json");
        }

        [TestMethod]
        public void TestShortUrlStatus()
        {
            var url = "http://www.albahari.com/threading/part4.aspx";
            var requestShortUrl = new SinaWeiboClientV1.ShortUrlShortenRequest { UrlLong = new List<string> { url } };
            var short_url = _sinaWeibo.ExecuteShortUrlShorten(requestShortUrl).Value.First().UrlShort;

            var requestShortUrlStatus = new SinaWeiboClientV1.ShortUrlShareStatusesRequest { UrlShort = short_url };
            var responseShortUrlStatus = _sinaWeibo.RequestShortUrlShareStatuses(requestShortUrlStatus);
            var result = responseShortUrlStatus.SaveAs("short_url/share/statuses.Response.json");
        }

        [TestMethod]
        public void TestShortUrlComments()
        {
            var url = "http://www.albahari.com/threading/part4.aspx";
            var requestShortUrl = new SinaWeiboClientV1.ShortUrlShortenRequest { UrlLong = new List<string> { url } };
            var short_url = _sinaWeibo.ExecuteShortUrlShorten(requestShortUrl).Value.First().UrlShort;

            var requestShortUrlComments = new SinaWeiboClientV1.ShortUrlCommentCommentsRequest { UrlShort = short_url };
            var responseShortUrlComments = _sinaWeibo.RequestShortUrlCommentComments(requestShortUrlComments);
            var result = responseShortUrlComments.SaveAs("short_url/comments.Response.json");
        }
        #endregion

        #region favorites
        [TestMethod()]
        public void TestFavoritesCreate()
        {
            WeiboClient target = _sinaWeibo;
            FavoritesCreateRequest request = new FavoritesCreateRequest { Id = "3391994462233661" };

            JsonResponse<Stream> actual;
            actual = target.RequestFavoritesCreate(request);
            actual.SaveAs("favorites/create.Response.json");
        }


        [TestMethod()]
        public void TestFavoritesDestroyId()
        {
            WeiboClient target = _sinaWeibo;
            FavoritesDestroyRequest request = new FavoritesDestroyRequest { Id = "3391994462233661" }; 
            
            JsonResponse<Stream> actual;
            actual = target.RequestFavoritesDestroy(request);
            actual.SaveAs("favorites/destroy/id.Response.json");
        }

        [TestMethod()]
        public void TestFavoritesDestroyBatch()
        {
            WeiboClient target = _sinaWeibo;
            FavoritesDestroyBatchRequest request = new FavoritesDestroyBatchRequest { Ids = "3391994462233661" }; 
   
            JsonResponse<Stream> actual;
            actual = target.RequestFavoritesDestroyBatch(request);
            actual.SaveAs("favorites/destroy_batch.Response.json");
        }
        
        [TestMethod()]
        public void TestRequestFavorites()
        {
            WeiboClient target = _sinaWeibo;
            FavoritesRequest request = new FavoritesRequest { Page = "0" }; 
           
            JsonResponse<Stream> actual;
            actual = target.RequestFavorites(request);
            actual.SaveAs("favorites.Response.json");
        }
        #endregion

        #region account
        [TestMethod()]
        public void TestAccountVerifyCredentials()
        {
            WeiboClient target = _sinaWeibo;
            AccountVerifyCredentialsRequest request = new AccountVerifyCredentialsRequest { }; 
            JsonResponse<Stream> expected = null; 
            JsonResponse<Stream> actual;
            actual = target.RequestAccountVerifyCredentials(request);
            actual.SaveAs("account/verify_credentials.Response.json");
        }

        [TestMethod()]
        public void TestAccountUpdateProfileImage()
        {
            WeiboClient target = _sinaWeibo;
            AccountUpdateProfileImageRequest request = new AccountUpdateProfileImageRequest
            {
                 Image=new FileUploadInfo
                 {
                     LocalPath = "e:\test.png",
                     StreamFactory = Task.Factory.StartNew<Stream>(() => File.OpenRead(@"e:\test.png"))
                 }
            }; 
            
            JsonResponse<Stream> actual;
            actual = target.RequestAccountUpdateProfileImage(request);
            actual.SaveAs("account/update_profile_image.Response.json");
        }

        [TestMethod()]
        public void TestAccountUpdateProfile()
        {
            WeiboClient target = _sinaWeibo;
            AccountUpdateProfileRequest request = new AccountUpdateProfileRequest
            {
                City = "1000",
                Gender = "m",
                Name = "testJDCB",
                Province = "0"
            }; 
            //JsonResponse<Stream> expected = null; 
            JsonResponse<Stream> actual;
            actual = target.RequestAccountUpdateProfile(request);
            actual.SaveAs("account/update_profile.Response.json");
        }

        [TestMethod()]
        public void TestAccountRateLimitStatus()
        {
            WeiboClient target = _sinaWeibo;
            AccountRateLimitStatusRequest request = new AccountRateLimitStatusRequest { }; 
            
            JsonResponse<Stream> actual;
            actual = target.RequestAccountRateLimitStatus(request);
            actual.SaveAs("account/rate_limit_status.Response.json");
        }

        [TestMethod()]
        public void TestAccountEndSession()
        {
            WeiboClient target = _sinaWeibo;
            AccountEndSessionRequest request = new AccountEndSessionRequest {  }; 
            
            JsonResponse<Stream> actual;
            actual = target.RequestAccountEndSession(request);
            actual.SaveAs("account/end_session.Response.json");
        }

        [TestMethod]
        public void TestAccountGetPrivacy()
        {
            var request = new SinaWeiboClientV1.AccountGetPrivacyRequest();
            var response = _sinaWeibo.RequestAccountGetPrivacy(request);
            var result = response.SaveAs("account/get_privacy.Response.json");
        }

        [TestMethod]
        [Ignore]
        public void TestAccountUpdatePrivacy()
        {
            var request = new SinaWeiboClientV1.AccountUpdatePrivacyRequest { Comment="1" };
            
            var response = _sinaWeibo.RequestAccountUpdatePrivacy(request);
            var result = response.SaveAs("account/update_privacy.Response.json");
        }
        #endregion

        #region socialgraph
        [TestMethod]
        public void TestFriendsIds()
        {
            var request = new SinaWeiboClientV1.SocialgraphFriendsIdsRequest { Id = _sinaWeibo.CurrentAuthToken.ContextData["UserID"] as string };
            var response = _sinaWeibo.RequestSocialgraphFriendsIds(request);
            var result = response.SaveAs("socialgraph/friendsIds.Response.json");
        }

        [TestMethod]
        public void TestFollowersIds()
        {
            var request = new SinaWeiboClientV1.SocialgraphFollowsIdsRequest { Id = _sinaWeibo.CurrentAuthToken.ContextData["UserID"] as string };
            var response = _sinaWeibo.RequestSocialgraphFollowsIds(request);
            var result = response.SaveAs("socialgraph/followersIds.Response.json");
        }
        #endregion

        #region tags
        [TestMethod]
        public void TestTagsCreate()
        {
            var request = new SinaWeiboClientV1.TagsCreateRequest { Tags = "tag1,tag2,tag3" };
            var response = _sinaWeibo.RequestTagsCreate(request);
            var result = response.SaveAs("tags/create.Response.json");
        }

        [TestMethod]
        public void TestTags()
        {
            var request = new SinaWeiboClientV1.TagsTagsRequest { UserId = _sinaWeibo.CurrentAuthToken.ContextData["UserID"] as string };
            var response = _sinaWeibo.RequestTagsTags(request);
            var result = response.SaveAs("tags/tags.Response.json");
        }

        [TestMethod]
        public void TestTagsDestory()
        {
            var requestCreate = new SinaWeiboClientV1.TagsCreateRequest { Tags = "tag" };
            var responseCreate = _sinaWeibo.ExecuteTagsCreate(requestCreate);
            var tagId = responseCreate.Value.First().Tagid;

            var requestDestory = new SinaWeiboClientV1.TagsDestroyRequest { TagId = tagId };
            var responseDestory = _sinaWeibo.RequestTagsDestroy(requestDestory);
            var result = responseDestory.SaveAs("tags/destory.Response.json");
        }

        [TestMethod]
        public void TestTagsSuggestions()
        {
            var request = new SinaWeiboClientV1.TagsSuggestionsRequest { Count = "2" };
            var response = _sinaWeibo.RequestTagsSuggestions(request);
            var result = response.SaveAs("tags/suggestions.Response.json");
        }

        [TestMethod]
        public void TestTagsDestoryBatch()
        {
            var requestCreate = new SinaWeiboClientV1.TagsCreateRequest { Tags = "tag001,tag002,tag003" };
            var responseCreate = _sinaWeibo.ExecuteTagsCreate(requestCreate);
            var tagIds = string.Join(",", responseCreate.Value.Select(t => t.Tagid));

            var request = new SinaWeiboClientV1.TagsDestroyBatchRequest { Ids = tagIds };
            var response = _sinaWeibo.RequestTagsDestroyBatch(request);
            var result = response.SaveAs("tags/destory_batch.Response.json");
        }
        #endregion

        #region blocks
        [TestMethod]
        public void TestBlocksCreate()
        {
            var request = new SinaWeiboClientV1.BlocksCreateRequest { UserId = "1795200753" };
            var response = _sinaWeibo.RequestBlocksCreate(request);
            var result = response.SaveAs("blocks/create.Response.json");
        }

        [TestMethod]
        public void TestBlocksDestroy()
        {
            var request = new SinaWeiboClientV1.BlocksDestroyRequest { UserId = "1795200753" };
            var response = _sinaWeibo.RequestBlocksDestroy(request);
            var result = response.SaveAs("blocks/destory.Response.json");
        }

        [TestMethod]
        public void TestBlocksExists()
        {
            var request = new SinaWeiboClientV1.BlocksExistsRequest { UserId = "2573705502" };
            var response = _sinaWeibo.RequestBlocksExists(request);
            var result = response.SaveAs("blocks/exists.Response.json");
        }

        [TestMethod]
        public void TestBlocksBlocking()
        {
            var requestCreate = new SinaWeiboClientV1.BlocksCreateRequest { UserId = "2573705502" };
            var responseCreate = _sinaWeibo.RequestBlocksCreate(requestCreate);
            requestCreate = new SinaWeiboClientV1.BlocksCreateRequest { UserId = "2559716940" };
            responseCreate = _sinaWeibo.RequestBlocksCreate(requestCreate);

            var request = new SinaWeiboClientV1.BlocksBlockingRequest { Count = "2" };
            var response = _sinaWeibo.RequestBlocksBlocking(request);
            var result = response.SaveAs("blocks/blocking.Response.json");
        }

        [TestMethod]
        public void TestBlocksBlockingIds()
        {
            var request = new SinaWeiboClientV1.BlocksBlockingIdsRequest { Count = "2" };
            var response = _sinaWeibo.RequestBlocksBlockingIds(request);
            var result = response.SaveAs("blocks/blockingIds.Response.json");
        }
        #endregion

        #region others
        [TestMethod]
        public void TestEmotions()
        {
            var request = new SinaWeiboClientV1.EmotionsRequest();
            var response = _sinaWeibo.ExecuteEmotions(request);
            Assert.IsTrue(response.Value.Count > 0);
        }


        //[TestMethod]
        //public void ModifyRequestXml()
        //{
        //    var dir = @"E:\Programming\VSProject2010\Codeplex\JsonDataContractBuilder\SinaJDCBClient\SinaWeiboClientV1\ContractSource";
        //    var files = Directory.GetFiles(dir, "*.xml", SearchOption.AllDirectories);
        //    foreach (var f in files)
        //    {
        //        XNamespace ns = "http://jdcb.codeplex.com/RequestDef.xsd";
        //        var xdoc = XDocument.Load(f);
        //        var comments = xdoc.Descendants(ns + "Comment").ToList();
        //        if (comments.First().Value.StartsWith("<para>"))
        //            continue;
        //        foreach (var cmmt in comments)
        //        {
        //            if (cmmt.Value.StartsWith("<para>"))
        //                continue;
        //            var lines = cmmt.Value.Split('\n');
        //            var content = string.Concat(lines.Select(s => "<para>" + s + "</para>\n").ToArray());
        //            var cdata = new XCData(content);
        //            cmmt.RemoveAll();
        //            cmmt.Add(cdata);
        //        }
        //        xdoc.Save(f);
        //    }
        //}
        #endregion

    }

  
}
